package nc.ui.pub.filesystem;

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;

import nc.bs.logging.Logger;
import nc.ui.sm.login.ClientAssistant;
import nc.vo.pub.filesystem.NCFileNode;

public class FileManageServletClient {
	public static NCFileNode uploadFile(String dsName, String pPath,
			String fileName, InputStream input, String creator, long fileLength)
			throws Exception {
		ObjectOutputStream oos = null;
		ObjectInputStream ois = null;
		NCFileNode newNode = null;
		StringBuilder sbUrl = new StringBuilder(ClientAssistant.getServerURL())
				.append("service/FileManageServlet");
		try {
			URL url = new URL(sbUrl.toString());
			URLConnection conn = url.openConnection();
			if (conn instanceof HttpURLConnection) {
				((HttpURLConnection) conn).setChunkedStreamingMode(2048);
			}
			conn.setDoInput(true);
			conn.setDoOutput(true);
			conn.setRequestProperty("content-type",
					"application/x-java-serialized-object,charset=utf-8");
			byte[] buf = new byte[2048];
			int len = -1;
			oos = new ObjectOutputStream(conn.getOutputStream());
			HashMap<String, String> headInfo = new HashMap<String, String>();
			headInfo.put("dsName", dsName);
			headInfo.put("operType", "upload");
			headInfo.put("parentPath", pPath);
			headInfo.put("fileName", fileName);
			headInfo.put("creator", creator);
			headInfo.put("fileLength", fileLength + "");
			oos.writeObject(headInfo);
			while ((len = input.read(buf)) != -1) {
				oos.write(buf, 0, len);
			}
			oos.flush();
			oos.close();
			ois = new ObjectInputStream(conn.getInputStream());
			Object obj = ois.readObject();
			if (obj instanceof Exception) {
				throw (Exception) obj;
			}
			newNode = (NCFileNode) obj;
		} finally {
			try {
				if (oos != null) {
					oos.close();
				}
				if (ois != null) {
					ois.close();
				}
			} catch (Exception e2) {
			}
		}
		return newNode;
	}

	public static void downloadFile(String dsName, String path, OutputStream out)
			throws Exception {
		ObjectOutputStream oos = null;
		InputStream in = null;
		StringBuilder sbUrl = new StringBuilder(ClientAssistant.getServerURL())
				.append("service/FileManageServlet");
		try {
			URL url = new URL(sbUrl.toString());
			URLConnection conn = url.openConnection();
			conn.setDoInput(true);
			conn.setDoOutput(true);
			conn.setRequestProperty("content-type",
					"application/x-java-serialized-object,charset=utf-8");
			oos = new ObjectOutputStream(conn.getOutputStream());
			HashMap<String, String> headInfo = new HashMap<String, String>();
			headInfo.put("dsName", dsName);
			headInfo.put("operType", "download");
			headInfo.put("path", path);
			oos.writeObject(headInfo);
			oos.flush();
			in = conn.getInputStream();
			byte[] buf = new byte[2048];
			int len = -1;
			while ((len = in.read(buf)) != -1) {
				out.write(buf, 0, len);
				out.flush();
			}

		} catch (Exception e) {
			Logger.error(e.getMessage(), e);
			e.printStackTrace();
		} finally {
			try {
				if (oos != null) {
					oos.close();
				}
				if (in != null) {
					in.close();
				}
			} catch (Exception e2) {
			}
		}
	}

	public static void showFileInWeb(String dsName, String path)
			throws Exception {
		StringBuilder sbUrl = new StringBuilder(ClientAssistant.getServerURL())
				.append("service/FileManageServlet?");
		sbUrl.append("dsName=").append(dsName);
		sbUrl.append("&path=").append(path);// (URLEncoder.encode(path, "GBK"));

		URL url = new URL(sbUrl.toString());
		ClientAssistant.showDocument(url, "_blank");
	}
}
